package com.mahees.portfolio.controller

import com.mahees.portfolio.domain.DevelopmentTool;
import com.mahees.portfolio.domain.DevelopmentToolTypeEnum;

import grails.validation.ValidationException;

class DevelopmentToolController {

	static allowedMethods = [save: "POST", update: "PUT", delete: "POST"]

	def list = {
		params.max = Math.min(params.max ? params.int('max') : 10, 100)

		withFormat {
			html {
				[developmentToolInstanceList: DevelopmentTool.list(params), developmentToolInstanceTotal: DevelopmentTool.count()]
			}
			json {
				render(contentType: "application/json", status: 200) {
					array {
						DevelopmentTool.list(params).each {
							dTool(id : it.id, name : it.name, description : it.description ?: '', url : it.url, toolType : it.toolType.type)
						}
					}
				}
			}
		}
	}

	def create = {
		def developmentToolInstance = new DevelopmentTool()
		developmentToolInstance.properties = params
		return [developmentToolInstance: developmentToolInstance]
	}

	def save = { DevelopmentToolCommand cmd ->
		if (!cmd.validate()) {
			withFormat {
				json {
					render(contentType: "application/json", status: 500) {
						array {
							cmd.errors.allErrors.each { e ->
								err(field: e.field, value: e.rejectedValue, message: e.defaultMessage)
							}
						}
					}
				}
			}
			return
		}

		DevelopmentTool developmentTool = new DevelopmentTool(
				name : cmd.name,
				description : cmd.description,
				url : cmd.url,
				toolType : cmd.toolType as DevelopmentToolTypeEnum
				);

		try {
			developmentTool.save(failOnError: true);
			withFormat {
				json {
					render(contentType: "application/json", status: 200)
				}
			}
		}
		catch(ValidationException ex) {
			withFormat {
				json {
					render(contentType: "application/json", status: 500) {
						message = ex.message
						fullMessage = ex.fullMessage
						errorList = array {
							ex.errors.allErrors.each { e ->
								err(field: e.field, value: e.rejectedValue, message: e.defaultMessage)
							}
						}
					}
				}
			}
		}
		catch (RuntimeException ex) {
			withFormat {
				json {
					render(contentType: "application/json", status: 500) {
						message = ex.message
						detailMessage = ex.detailMessage
					}
				}
			}
		}
	}

	def show = {
		def developmentToolInstance = DevelopmentTool.get(params.id)
		if (!developmentToolInstance) {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'developmentTool.label', default: 'DevelopmentTool'), params.id])}"
			redirect(action: "list")
		}
		else {
			[developmentToolInstance: developmentToolInstance]
		}
	}

	def edit = {
		def developmentToolInstance = DevelopmentTool.get(params.id)
		if (!developmentToolInstance) {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'developmentTool.label', default: 'DevelopmentTool'), params.id])}"
			redirect(action: "list")
		}
		else {
			return [developmentToolInstance: developmentToolInstance]
		}
	}

	def update = {
		def developmentToolInstance = DevelopmentTool.get(params.id)
		if (developmentToolInstance) {
			if (params.version) {
				def version = params.version.toLong()
				if (developmentToolInstance.version > version) {

					developmentToolInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [
						message(code: 'developmentTool.label', default: 'DevelopmentTool')]
					as Object[], "Another user has updated this DevelopmentTool while you were editing")
					render(view: "edit", model: [developmentToolInstance: developmentToolInstance])
					return
				}
			}
			developmentToolInstance.properties = params
			if (!developmentToolInstance.hasErrors() && developmentToolInstance.save(flush: true)) {
				flash.message = "${message(code: 'default.updated.message', args: [message(code: 'developmentTool.label', default: 'DevelopmentTool'), developmentToolInstance.id])}"
				redirect(action: "show", id: developmentToolInstance.id)
			}
			else {
				render(view: "edit", model: [developmentToolInstance: developmentToolInstance])
			}
		}
		else {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'developmentTool.label', default: 'DevelopmentTool'), params.id])}"
			redirect(action: "list")
		}
	}

	def delete = {
		def developmentToolInstance = DevelopmentTool.get(params.id)
		if (developmentToolInstance) {
			try {
				developmentToolInstance.delete(flush: true)
				flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'developmentTool.label', default: 'DevelopmentTool'), params.id])}"
				redirect(action: "list")
			}
			catch (org.springframework.dao.DataIntegrityViolationException e) {
				flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'developmentTool.label', default: 'DevelopmentTool'), params.id])}"
				redirect(action: "show", id: params.id)
			}
		}
		else {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'developmentTool.label', default: 'DevelopmentTool'), params.id])}"
			redirect(action: "list")
		}
	}
}


class DevelopmentToolCommand {
	String name
	String description
	String url
	String toolType

	static constraints = {
		name	(nullable : false, blank : false, size: 2..50); //TODO: 50 for testing
		description	(nullable : true, blank : true);
		url		(nullable : false, blank : false, minSize: 2, url : true);
		toolType (nullable: false)
	}
}